<script setup>

import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";

const insertShow = ref(false)
const openInsert = () => {
  insertShow.value = true
}


const imageForm = ref({});
const imageUrl = ref(null);
const upload = (data) => {
  const form = new FormData();
  form.append('file', data.file);

  axios.post('/server/picture/upload', form)
      .then(res=>{
        res = res.data;
        //将返回的数据（原始名称，新名称）放到imageForm对象中
        imageForm.value.originTitle = res.data[0];
        imageForm.value.url = res.data[1];
        imageUrl.value = "/server/picture/getImage?url="+res.data[1];
  })
}

const submit = () => {
  axios.post('/server/picture/save', imageForm.value).then(res=>{
    res = res.data;
    insertShow.value = false;
    ElMessage({
      message: res.msg,
      type: 'success',
    })
  })
}

const imageList = ref([]);
const list = () => {
  axios.get('/server/picture/list').then(res=>{
    res = res.data;
    imageList.value = res.data;
  })
}
onMounted(()=>{
  list();
})
</script>

<template>
  <div>
    <el-button type="primary" size="small" @click="openInsert">新增</el-button>
    <el-button type="primary" size="small">刷新</el-button>
  </div>
  <div>
    <el-dialog
        v-model="insertShow"
        title="图片新增"
        width="500"
    >
      <span>
        <el-form :model="form" label-width="auto" style="max-width: 600px">
          <el-form-item label="标题">
            <el-input v-model="imageForm.title"/>
          </el-form-item>
          <el-form-item label="图片">
            <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :http-request="upload"
                v-model:file-list="imageList"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar"  alt=""/>
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="insertShow=false">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>

  <el-table :data="imageList" style="width: 100%">
    <el-table-column prop="title" label="标题" width="180" />
    <el-table-column prop="url" label="图片" width="180" >
      <template #default="scope">
        <img :src="'/server/picture/getImage?url='+scope.row.url" alt="" style="width: 100px;height: 100px">
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180" >
      <template #default="scope">
        <el-button>修改</el-button>
        <el-button type="danger">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

</template>

<style scoped>
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
</style>